package v1

import (
	"adminHome/models"
	"adminHome/pkg/app"
	"adminHome/pkg/e"
	"adminHome/pkg/util"
	"github.com/astaxie/beego/validation"
	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
	"net/http"
)

func GetStockList(c *gin.Context) {
	appG := app.Gin{c}

	storageId := com.StrTo(c.PostForm("storage_id")).MustInt()
	productId := com.StrTo(c.PostForm("product_id")).MustInt()

	data := make(map[string]interface{})
	response := make(map[string]interface{})

	if storageId != 0 {
		data["storage_id"] = storageId
	}
	if productId != 0 {
		data["product_id"] = productId
	}

	list, _ := models.GetStockList(util.GetPage(c), util.GetPageSize(c), data)
	count, _ := models.GetStockTotal(data)
	response["list"] = list
	response["total"] = count

	code := e.SUCCESS
	appG.Response(http.StatusOK, code, response)
}

func GetStock(c *gin.Context) {
	appG := app.Gin{c}

	id := com.StrTo(c.Query("id")).MustInt()

	valid := validation.Validation{}
	valid.Required(id, "id").Message("id为必填项")
	code := e.INVALID_PARAMS
	if valid.HasErrors() {
		app.MarkErrors(valid.Errors)
		appG.Response(http.StatusOK, code, nil)
		return
	}
	stock, err := models.GetStock(id)
	if err != nil {
		code = e.ERROR_NOT_EXIST_STOCK
		appG.Response(http.StatusOK, code, nil)
		return
	}
	code = e.SUCCESS
	appG.Response(http.StatusOK, code, stock)
}

func EditStockNum(c *gin.Context) {
	appG := app.Gin{c}

	id := com.StrTo(c.PostForm("id")).MustInt()
	num := com.StrTo(c.PostForm("num")).MustInt()

	valid := validation.Validation{}
	valid.Required(id, "id").Message("id为必填项目")
	valid.Required(num, "num").Message("数量为必填项目")
	code := e.INVALID_PARAMS
	if valid.HasErrors() {
		app.MarkErrors(valid.Errors)
		appG.Response(http.StatusOK, code, nil)
		return
	}

	if !models.ExistStockById(id) {
		code = e.ERROR_NOT_EXIST_STOCK
		appG.Response(http.StatusOK, code, nil)
		return
	}

	if !models.EditStockNum(id, num) {
		code = e.ERROR_UPDATE_STOCK
		appG.Response(http.StatusOK, code, nil)
		return
	}
	code = e.SUCCESS
	appG.Response(http.StatusOK, code, nil)
}

func AddStock(c *gin.Context) {
	appG := app.Gin{c}
	storageId := com.StrTo(c.PostForm("storage_id")).MustInt()
	productId := com.StrTo(c.PostForm("product_id")).MustInt()
	num := com.StrTo(c.PostForm("num")).MustInt()

	valid := validation.Validation{}
	valid.Required(storageId, "storage_id").Message("仓库不能为空")
	valid.Required(productId, "product_id").Message("产品不能为空")
	valid.Required(num, "num").Message("库存数量不能为空")

	code := e.INVALID_PARAMS
	if valid.HasErrors() {
		app.MarkErrors(valid.Errors)
		appG.Response(http.StatusOK, code, nil)
		return
	}

	storage, _ := models.GetStorageById(storageId)
	product, _ := models.GetProductById(productId)

	if models.ExistStock(storageId, productId) {
		code = e.ERROR_EXIST_STOCK
		appG.Response(http.StatusOK, code, nil)
		return
	}
	data := make(map[string]interface{})
	data["storage_id"] = storageId
	data["storage_name"] = storage.Name
	data["product_id"] = product.ID
	data["product_name"] = product.Name
	data["product_spec"] = product.Spec
	data["num"] = num

	if models.AddStock(data) {
		code = e.SUCCESS
	}

	appG.Response(http.StatusOK, code, nil)

}
